package com.gitee.elasticjob.listener;

import com.dangdang.ddframe.job.executor.ShardingContexts;
import com.dangdang.ddframe.job.lite.api.listener.AbstractDistributeOnceElasticJobListener;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ElasticJobHandleListener extends AbstractDistributeOnceElasticJobListener {
//    @Resource
//    private TaskRepository taskRepository;

    public ElasticJobHandleListener(long startedTimeoutMilliseconds, long completedTimeoutMilliseconds) {
        super(startedTimeoutMilliseconds, completedTimeoutMilliseconds);
    }

    @Override
    public void doBeforeJobExecutedAtLastStarted(ShardingContexts shardingContexts) {
        log.info("定时任务TaskId:{},JobName:{} ----> 开始执行", shardingContexts.getTaskId(), shardingContexts.getJobName());
        log.info("定时任务TaskId:{},JobName:{} ----> jobParameter:{}", shardingContexts.getTaskId(), shardingContexts.getJobName(), shardingContexts.getJobParameter());
        log.info("定时任务TaskId:{},JobName:{} ----> shardingItemParameters:{}", shardingContexts.getTaskId(), shardingContexts.getJobName(), shardingContexts.getShardingItemParameters());
        log.info("定时任务TaskId:{},JobName:{} ----> shardingTotalCount:{}", shardingContexts.getTaskId(), shardingContexts.getJobName(), shardingContexts.getShardingTotalCount());
        log.info("定时任务TaskId:{},JobName:{} ----> currentJobEventSamplingCount:{}", shardingContexts.getTaskId(), shardingContexts.getJobName(), shardingContexts.getCurrentJobEventSamplingCount());
        log.info("定时任务TaskId:{},JobName:{} ----> jobEventSamplingCount:{}", shardingContexts.getTaskId(), shardingContexts.getJobName(), shardingContexts.getJobEventSamplingCount());
    }

    @Override
    public void doAfterJobExecutedAtLastCompleted(ShardingContexts shardingContexts) {
        //任务执行完成后更新状态为已执行
//        JobTask jobTask = taskRepository.findOne(Long.valueOf(shardingContexts.getJobParameter()));
//        jobTask.setStatus(1);
//        taskRepository.save(jobTask);
        log.info("定时任务TaskId:{},JobName:{} ----> 执行结束", shardingContexts.getTaskId(), shardingContexts.getJobName());
    }
}
